"""
运行程序，打开浏览器访问以下url：
http://127.0.0.1:8080

"""
import time
import select
import socket
import datetime

HOST, PORT = "localhost", 9999

server = socket.socket()
server.bind((HOST, PORT))
server.listen()
server.setblocking(False)

print('Serving HTTP on port %s ...' % PORT)

request_list = []


def running():
    while True:
        try:
            request, address = server.accept()
            print(request)
            print(f'[{datetime.datetime.now()}] connection success')
            request_list.append(request)
        except BlockingIOError:
            time.sleep(0.5)
            pass

        if request_list:
            r_list, w_list, x_list = select.select(request_list, [], [], 0.5)
            for request in r_list:
                received = request.recv(1024)
                print(request)
                print(f'[{datetime.datetime.now()}] receive success, data: {received}')
                request.sendall(b'request success')
                request.close()
                request_list.remove(request)


running()
